package com.enniswu.weitter.moment.postlist;

import android.support.annotation.Nullable;

import com.enniswu.weitter.base.BasePresenter;
import com.enniswu.weitter.data.local.DBSupport;
import com.enniswu.weitter.data.remote.PostLoader;
import com.enniswu.weitter.data.remote.UserLoader;
import com.enniswu.weitter.entity.Post;
import com.enniswu.weitter.entity.User;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PostListPresenter<V extends PostListContract.View> extends BasePresenter<V> implements PostListContract.Presenter {

    protected int type;
    protected int orientation;
    protected PostLoader pLoader;
    protected UserLoader uLoader;

    public PostListPresenter(V view, int type, int orientation) {
        super(view);
        this.type = type;
        this.orientation = orientation;
        pLoader = new PostLoader();
        uLoader = new UserLoader();
    }

    @Override
    public void setOrientation(int orientation) {
        this.orientation = orientation;
    }

    @Override
    public void getPosts(int offset) {
        if (offset == 1) {
            DBSupport.getInstance().deleteAll(type);
            view.clearPosts();
            view.notifyDataSetChanged();
        }
        pLoader.getPostIds(type, orientation, offset, 10, new AbstractCallback<List<Integer>>() {
            @Override
            public void onSuccess(@Nullable List<Integer> integers) {
                if (integers == null || integers.isEmpty()) {
                    if (view != null) {
                        view.finishLoadData();
                    }
                    return;
                }
                getPosts(integers);
            }
        });
    }

    @Override
    public void start() {
        List<Post> posts = DBSupport.getInstance().findAll(orientation);
        if (posts.isEmpty())  {
            view.refresh();
            return;
        }
        view.addPosts(posts);
        view.notifyDataSetChanged();
    }

    private boolean isAdded;

    public void getPosts(final List<Integer> ids) {
        final List<Post> posts = Collections.synchronizedList(new ArrayList<Post>(ids.size()));
        isAdded = false;
        for (int id : ids) {
            pLoader.getPost(id, type, new AbstractCallback<Post>() {
                @Override
                public void onFailure(String msg) {
                    super.onFailure(msg);
                    posts.add(null);
                }

                @Override
                public void onSuccess(@Nullable Post post) {
                    posts.add(post);
                    if (post != null) {
                        getUser(post, post.getUserId());
                        DBSupport.getInstance().insertOrUpdate(post);
                    }
                    if (posts.size() == ids.size() && !isAdded) {
                        isAdded = true;
                        posts.removeAll(Collections.singleton(null));
                        if (view == null)
                            return;
                        view.addPosts(posts);
                        view.notifyDataSetChanged();
                        view.finishLoadData();
                    }
                }
            });
        }
    }

    public void getUser(final Post post, int id) {
        uLoader.getInfo(id, new AbstractCallback<User>() {
            @Override
            public void onSuccess(@Nullable User user) {
                DBSupport.getInstance().insertOrUpdate(user);
                if (user != null) {
                    view.notifyItemUserChanged(post);
                }
            }
        });
    }
}
